package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.OrganizationConvert;
import cn.com.blueInfo.bpm.person.entity.Organization;
import cn.com.blueInfo.bpm.person.repository.OrganizationRepository;
import cn.com.blueInfo.bpm.person.request.OrganizationDTO;
import cn.com.blueInfo.bpm.person.response.OrganizationVO;
import cn.com.blueInfo.bpm.person.service.OrganizationService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 组织结构信息表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationRepository organizationRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private OrganizationConvert organizationConvert;

    /**
     * 新增组织结构信息
     * @param organizationDTO 组织结构信息参数
     */
    @Override
    public ResultInfo<Integer> addOrganization(OrganizationDTO organizationDTO) {
        Organization organization = organizationConvert.dtoToEntity(organizationDTO);
        organization.setId(snowflakeIdGenerator.nextId());
        Integer addSize = organizationRepository.createOrganization(organization);
        return ResultInfo.<Integer>builder().data(addSize).message("组织结构信息成功新增 {} 条").build();
    }

    /**
     * 批量新增组织结构信息
     * @param organizationDTOList 组织结构信息参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddOrganization(List<OrganizationDTO> organizationDTOList) {
        List<Organization> organizationList = organizationConvert.dtoToEntity(organizationDTOList);
        for (Organization organization : organizationList) organization.setId(snowflakeIdGenerator.nextId());
        Integer addSize = organizationRepository.batchCreateOrganization(organizationList);
        return ResultInfo.<Integer>builder().data(addSize).message("组织结构信息成功新增 {} 条").build();
    }

    /**
     * 删除组织结构信息
     * @param organizationId 组织结构信息ID
     */
    @Override
    public ResultInfo<Integer> removeOrganization(Long organizationId) {
        Integer removeSize = organizationRepository.deleteOrganization(organizationId);
        return ResultInfo.<Integer>builder().data(removeSize).message("组织结构信息成功删除 {} 条").build();
    }

    /**
     * 批量删除组织结构信息
     * @param organizationIdList 组织结构信息ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveOrganization(List<Long> organizationIdList) {
        Integer removeSize = organizationRepository.batchDeleteOrganization(organizationIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("组织结构信息成功删除 {} 条").build();
    }

    /**
     * 更新部分组织结构信息
     * @param organizationUpdatePartialDTO 组织结构信息参数
     */
    @Override
    public ResultInfo<Integer> modifyOrganizationPartial(
            OrganizationDTO.UpdatePartialDTO organizationUpdatePartialDTO) {
        Organization organization = organizationConvert.dtoToEntity(organizationUpdatePartialDTO);
        Integer modifySize = organizationRepository.updateOrganizationPartial(organization);
        return ResultInfo.<Integer>builder().data(modifySize).message("组织结构信息成功更新 {} 条").build();
    }

    /**
     * 更新全部组织结构信息
     * @param organizationDTO 组织结构信息参数
     */
    @Override
    public ResultInfo<Integer> modifyOrganization(OrganizationDTO organizationDTO) {
        Organization organization = organizationConvert.dtoToEntity(organizationDTO);
        Integer modifySize = organizationRepository.updateOrganization(organization);
        return ResultInfo.<Integer>builder().data(modifySize).message("组织结构信息成功更新 {} 条").build();
    }

    /**
     * 查询一条组织结构信息
     * @param organizationId 组织结构信息ID
     */
    @Override
    public ResultInfo<OrganizationVO> queryOrganizationById(Long organizationId) {
        Organization organization = organizationRepository.getOrganizationById(organizationId);
        OrganizationVO organizationVO = organizationConvert.entityToVO(organization);
        return ResultInfo.<OrganizationVO>builder().data(organizationVO).message("组织结构信息查询成功").build();
    }

    /**
     * 分页查询组织结构信息
     * @param organizationQueryDTO 组织结构信息参数
     */
    @Override
    public ResultInfo<Page<OrganizationVO>> queryOrganizationPage(OrganizationDTO.QueryDTO organizationQueryDTO) {
        Page<Organization> organizationPage = organizationRepository.getOrganizationPage(organizationQueryDTO);
        Page<OrganizationVO> resultData = organizationConvert.convertPage(organizationPage);
        return ResultInfo.<Page<OrganizationVO>>builder()
                .data(resultData)
                .message("组织结构信息分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 组织结构信息 数量
     * @param organizationQueryDTO 组织结构信息参数
     */
    @Override
    public ResultInfo<Long> queryOrganizationCount(OrganizationDTO.QueryDTO organizationQueryDTO) {
        Long resultCount = organizationRepository.getOrganizationCount(organizationQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("组织结构信息数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 组织结构信息
     * @param organizationQueryDTO 组织结构信息参数
     */
    @Override
    public ResultInfo<List<OrganizationVO>> queryOrganizationList(OrganizationDTO.QueryDTO organizationQueryDTO) {
        List<Organization> organizationList = organizationRepository.getOrganizationList(organizationQueryDTO);
        List<OrganizationVO> resultData = organizationConvert.entityListToVoList(organizationList);
        return ResultInfo.<List<OrganizationVO>>builder()
                .data(resultData)
                .message("组织结构信息分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
